﻿using System;
using System.Data;
using System.IO;
using System.Web;
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.MailMerging;
using LBSoft.Common.DotNetCode;

namespace LBSoft.Common.DotNetOutWord
{
    public class WordPrint
    {
        /// <summary>
        /// 在线导出word
        /// </summary>
        /// <param name="filePath">模板路径</param>
        /// <param name="fileName">模板文件名</param>
        /// <param name="Data">数据源</param>
        /// <param name="Page">当前页</param>
        public static void WordPrintMethod(string filePath, string fileName, DataSet Data, System.Web.UI.Page Page)
        {

            string templatePath = filePath + fileName;
            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出
            var docStream = new MemoryStream();
            doc.Save(Page.Response, HttpUtility.UrlEncode(fileName), Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));

        }
        /// <summary>
        /// 在线导出word
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="Data"></param>
        /// <param name="Page"></param>
        public static void WordPrintMethod(string filePath, string fileName, DataSet Data, HttpContext con)
        {

            string templatePath = filePath + fileName;
            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出
            var docStream = new MemoryStream();
            doc.Save(con.Response, HttpUtility.UrlEncode(fileName), Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));

        }

        /// <summary>
        /// 在线导出word
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="Data"></param>
        /// <param name="Page"></param>
        public static void WordPrintMethod2(string filePath, string fileName,string Fname, DataSet Data, HttpContext con)
        {

            string templatePath = filePath + fileName;
            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出
            var docStream = new MemoryStream();
            doc.Save(con.Response, HttpUtility.UrlEncode(Fname), Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));

        }

        /// <summary>
        /// 在线导出word
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="Data"></param>
        /// <param name="Page"></param>
        public static void WordPrintMethod3(string filePath, string fileName, string Fname, DataSet Data, HttpContext con)
        {

            string templatePath = filePath + fileName;
            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出
            var docStream = new MemoryStream();
            //doc.Save(con.Response, HttpUtility.UrlEncode(Fname), Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));

            doc.Save(System.Web.HttpContext.Current.Server.MapPath(filePath + Fname), Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));


        }


        /// <summary>
        /// 导出word
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="Data"></param>
        /// <param name="outDocName">导出word 文件名</param>
        public static void WordPrintMethod2(string filePath, string fileName, DataSet Data, string outDocName)
        {
            string templatePath = filePath + fileName;
            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出 
            doc.Save(System.Web.HttpContext.Current.Server.MapPath(outDocName), Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));
        }

        /// <summary>
        /// 调用Microsoft.Office组件将doc转换为pdf
        /// </summary>
        /// <param name="docFullPath">要转换的doc路径</param>
        /// <param name="pdfFullPath">要保存的pdf路径</param>
        /// <returns></returns>
        //public static bool WordToPDF(string docFullPath, string pdfFullPath)
        //{
        //    bool result = false;
        //    Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        //    Microsoft.Office.Interop.Word.Document document = null;
        //    try
        //    {
        //        application.Visible = false;
        //        document = application.Documents.Open(docFullPath);
        //        document.ExportAsFixedFormat(pdfFullPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
        //        result = true;
        //    }
        //    catch (Exception e)
        //    {
        //        LogHelper log = new LogHelper("WordToPDF");
        //        log.WriteLog(e.ToString());
        //        result = false;
        //    }
        //    finally
        //    {
        //        document.Close();
        //    }
        //    return result;
        //}

        /// <summary>
        /// 在线导出pdf
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="Data"></param>
        /// <param name="con"></param>
        /// <param name="outPdfName">导出pdf 文件名</param>
        public static void WordPrintMethod(string filePath, string fileName, DataSet Data, HttpContext con, string outPdfName)
        {

            string templatePath = filePath + fileName;
            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出
            var docStream = new MemoryStream();
            doc.Save(con.Response, HttpUtility.UrlEncode(outPdfName), Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Pdf));

        }

        /// <summary>
        /// 保存pdf
        /// </summary>
        /// <param name="templatePath">模板路径</param>
        /// <param name="Data">数据源</param>
        /// <param name="SavePath"></param>
        /// <param name="outPdfName">导出pdf文件名</param>
        public static void WordSavePdfMethod(string templatePath, DataSet Data, string SavePath, string outPdfName)
        {


            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);
            //输出
            //var docStream = new MemoryStream(); 
            string outputPath = SavePath + outPdfName;
            //FontSettings.SetFontsFolder("E:\\项目站点\\Fonts", false);
            doc.Save(System.Web.HttpContext.Current.Server.MapPath(outputPath), Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Pdf));
        }
        /// <summary>
        /// 保存pdf
        /// </summary>
        /// <param name="templatePath">模板路径</param>
        /// <param name="Data">数据源</param>
        /// <param name="SavePath"></param>
        /// <param name="outPdfName">导出pdf文件名</param>
        public static Document CreateWordsDocument(Document document, DataSet Data, string templatePath, bool isNew)
        {

            //获取全部信息
            var ds = Data;
            //载入模板
            var doc = new Document(System.Web.HttpContext.Current.Server.MapPath(templatePath));
            doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
            doc.MailMerge.ExecuteWithRegions(ds);

            if (!isNew)
            {
                document.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
            }
            else
            {
                document = doc;
            }
            return document;
            ////输出
            //var docStream = new MemoryStream();
            //string outputPath = SavePath + outPdfName;
            //doc.Save(System.Web.HttpContext.Current.Server.MapPath(outputPath), Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Pdf));
        }
    }
    class HandleMergeFieldInsertDocument : IFieldMergingCallback
    {
        //文本处理在这里，如果写在这一块，则不起作用 
        void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
        {
        }
        //图片处理在这里 
        /// <summary>
        /// This is called when mail merge engine encounters Image:XXX merge field in the document.
        /// You have a chance to return an Image object, file name or a stream that contains the image.
        /// </summary>
        void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        {
            if (args.DocumentFieldName.Equals("PHOTO") || args.DocumentFieldName.Equals("PHOTOSCAN"))
            {
                // 使用DocumentBuilder处理图片的大小
                DocumentBuilder builder = new DocumentBuilder(args.Document);
                builder.MoveToMergeField(args.FieldName);
                Shape shape = builder.InsertImage(args.FieldValue.ToString());
                // 设置x,y坐标和高宽.
                shape.Left = 0;
                shape.Top = 0;
                shape.Width = 70;
                shape.Height = 160;
            }
        }
    }
}
